#include "Mmc.h"
#include "Debug.h"
#include "FruInfo.h"
#include "Sdr.h"
#include "drv_init.h"
#include "ConfigInfo.h"
#include "Common_define.h"
#include "Command_Define.h"
#include "MmcLed.h"
#include "i2c_module_interface.h"
#include "Sensor_HotSwap.h"
#include "Sensor_Threshold.h"
#include "CmdHandler.h"
#include "Drv_Init.h"
#include "E_Key.h"
#include "Shell.h"
#include "Driver_i2c.h"
#include "Config.h"
#if TEST
#include "Driver_ek.h"
#endif

uint8 gWatchDogSwitch = 1;
uint8 gDelay = 1; 

unsigned char  g_Owerner_IPMB_Addr;
unsigned char  g_Owerner_IPMB_Type;
unsigned char  g_Owerner_Sit_Num;
unsigned char  g_Owerner_SIT_TYPE;

extern uint8 TempSensor_Scan(Sensor* pTSensor);
extern uint8 Get_Reset_Status(void);

Mmc g_Mmc;
int Mmc_PostMsg(uint8 object, uint8 msgID, uint32 param1, uint32 param2)
{
	int iResult = ERR_SUCCESS;
	if(!QUEUE_isFull(&g_Mmc.m_msgQueue))
	{
		Message msg;
		msg.m_ObjectID = object;
		msg.m_MsgID = msgID;
		msg.m_Param1 = param1;
		msg.m_Param2 = param2;
		
		QUEUE_add(&g_Mmc.m_msgQueue, &msg);
	}
	else
	{
		TRACE_LEVEL(TRACE_WARNING, ("%s() Msg Queue is Full!\n", _FUNC_)); 
		
		iResult = ERR_FAILED;
		Assert(False);
	}
	
	return iResult;
}

TimerManager* Mmc_TimerManager(void)
{
	return &g_Mmc.m_TimerManager;
}

Ipmb* Mmc_GetIpmb(uint8 iicChannel)
{
	if(I2C_CHANNEL_UARTIPMB == iicChannel)
	{
		return &g_Mmc.m_UartIpmb;
	}
	else
	{
		return &g_Mmc.m_Ipmb;
	}
}

Led* Mmc_GetLed(uint8 ledType)
{
	if(BLUE_LED == ledType)
	{
		return &g_Mmc.m_BlueLed;
	}
	else if(LED1_LED == ledType)
	{
		return &g_Mmc.m_Led1;
	}
	else
	{
		//Assert(FALSE);
	}

	return Null;
}

void Mmc_AddSensor(Mmc* pMmc, Sensor* pSensor)
{
	int i = 0;

	for(i = 0; i < MAX_SENSOR_COUNT; i++)
	{
		if(Null == pMmc->m_pSensor[i])
		{
			pMmc->m_pSensor[i] = pSensor;
			return ;
		}
	}

	Assert(False);
}

void Mmc_SensorDetect(Mmc* pMmc)
{
	Sensor* pSensor = Null;
	int i = 0;

	for(i = 0; i < MAX_SENSOR_COUNT; i++)
	{
		pSensor = pMmc->m_pSensor[i];
		if(pSensor)
		{
			if(pSensor->m_isScanEnable)
			{
				pSensor->m_RawValue = pSensor->Scan(pSensor);
			}
			if(ERR_SUCCESS == Sensor_SendEventMsg(pSensor))
			{
				break;
			}
		}
	}
}
void Mmc_TimerOut(SwTimer* timer, Mmc* pMmc)
{
	TRACE_LEVEL(TRACE_MAIN, ("\nConfig/Fru Timer timeout, Post msg[%d] to msg queue\n", MMC_MSG_INIT));
	if(!pMmc->m_isTimerDisable)
	{
		Mmc_PostMsg(OBJECT_MMC, MMC_MSG_INIT, 0, 0);
	}
}

Sensor* Mmc_GetSensor(uint8 sensorNum)
{
	int i = 0;
	Sensor* pSensor = Null;
	
	for(i = 0; i < MAX_SENSOR_COUNT; i++)
	{
		pSensor = g_Mmc.m_pSensor[i];
		if(pSensor && sensorNum == pSensor->m_SensorId)
		{
			return pSensor;
		}
	}

	//Assert(False);
	return Null;
}

void Mmc_SensorReset()
{
	int i = 0;
	Sensor* pSensor = Null;
	
	for(i = 0; i < MAX_SENSOR_COUNT; i++)
	{
		pSensor = g_Mmc.m_pSensor[i];
		if(pSensor)
		{
			pSensor->m_ReceiverValue = pSensor->m_DefaultValue;
			memset(&pSensor->m_ReceiverEventData, 0, sizeof(EventData));
		}
	}
}

void Mmc_SetSensorCrb(Crb* pCrb)
{
	int i = 0;
	Sensor* pSensor = Null;
	
	for(i = 0; i < MAX_SENSOR_COUNT; i++)
	{
		pSensor = g_Mmc.m_pSensor[i];
		if(pSensor && pSensor->m_pCrb != pCrb)
		{
			Crb_Reset(pSensor->m_pCrb);
			pSensor->m_pCrb = pCrb;
		}
	}
}


void Mmc_SetEventReceiver(uint8 eventReceiver, uint8 lun)
{
	int i = 0;
	Sensor* pSensor = Null;
	uint8 addr = eventReceiver >> 1;
	
	Trace("%s() Address = 0x%02x, lun=%d\n", _FUNC_, eventReceiver, lun);
	g_Mmc.m_ReceiverAddress = eventReceiver;
	g_Mmc.m_ReceiverLun = lun;
	
	if(addr == CM_IPMB_ADDR)
	{
		g_Mmc.m_ReqCrb.m_RemoteAddr = addr;
		g_Mmc.m_ReqCrb.m_RemoteLun = lun;
		Mmc_SetSensorCrb((Crb*)&g_Mmc.m_ReqCrb);
	}
	else if(addr == g_Owerner_IPMB_Addr)
	{
		g_Mmc.m_UartReqCrb.m_RemoteAddr = addr;
		g_Mmc.m_UartReqCrb.m_RemoteLun = lun;
		Mmc_SetSensorCrb((Crb*)&g_Mmc.m_UartReqCrb);
	}
	
	for(i = 0; i < MAX_SENSOR_COUNT; i++)
	{
		pSensor = g_Mmc.m_pSensor[i];
		if(pSensor)
		{
			pSensor->m_isEventMsgEnable = 1;
		}
	}
}

void Mmc_CreateSensor(Mmc* pMmc, Sdr* pSdr)
{
	int i = 0;
	int k = 0;
	Sensor* pSensor = Null;
	uint8 sensorType = 0;
	uint8 shareCount = 0;
	uint8 eventType = 0;
	SensorFun scan = Null;
	GetEventDataFun GetEventData = Null;
	uint8 defaultValue = 0;
	
	for(i = 0; i < pSdr->m_RecordCount; i++)
	{
		if(Null ==  pSdr->m_pRecordArray[i]) continue;

		shareCount = 0;
		if(SDR_COMPACT_SENSOR == pSdr->m_pRecordArray[i]->m_Type)
		{
			sensorType = ((CompactSensorRecord*)pSdr->m_pRecordArray[i])->m_SensorType;
			eventType = ((CompactSensorRecord*)pSdr->m_pRecordArray[i])->m_EventType;
			shareCount = ((CompactSensorRecord*)pSdr->m_pRecordArray[i])->m_ShareCount;
		}
		else if(SDR_FULL_SENSOR == pSdr->m_pRecordArray[i]->m_Type)
		{
			sensorType = ((FullSensorRecord*)pSdr->m_pRecordArray[i])->m_SensorType;
			eventType = ((FullSensorRecord*)pSdr->m_pRecordArray[i])->m_EventType;
			
		}
		else
		{
			continue;
		}

		scan = (SensorFun)Null;
		GetEventData = Null;
		switch(sensorType)
		{
			case TEMPERATURE_SENSOR:
				scan = (SensorFun)TempSensor_Scan;
				defaultValue = 0;
				break;
			case MODULE_HOT_SWAP:
				scan = (SensorFun)HowSwapSensor_Scan;
				defaultValue = HANDLE_OPENED;
				break;
			case VOLTAGE_SENSOR:
				scan = (SensorFun)HowSwapSensor_Scan;
				defaultValue = HANDLE_OPENED;
				break;
			default:
				Assert(False);
				break;
		}
		
		switch(eventType)
		{
			case ET_THRESHOLD:
				GetEventData = (GetEventDataFun)Sensor_ThresholdGetEventData;
				break;
			default:
				GetEventData = (GetEventDataFun)Sensor_GetEventData;
				break;
		}

		if(scan && GetEventData)
		{
			for(k = 0; k <= shareCount; k++)
			{
				pSensor = Sensor_New(pMmc->m_Sensor, MAX_SENSOR_COUNT);
				if(pSensor)
				{
					Sensor_Init(pSensor
						, pSdr->m_pRecordArray[i]
						, k
						, scan
						, GetEventData
						, (Crb*)&pMmc->m_ReqCrb
						, Null
						, defaultValue
						);
					Mmc_AddSensor(pMmc, pSensor);
				}
				else
				{
					Assert(pSensor);
				}
			}	
		}
		else
		{
			Assert(False);
		}
				
	}
}

int Mmc_CreateSdrRepository(Mmc* pMmc, ConfigInfo* pConfigInfo)
{
	int i = 0;
	Sdr* pSdr = &pMmc->m_Sdr;
	
	const static SensorItem g_SensorItem[] = 
	{ 
		{ET_SPECIFIC, HOTSWAP_SENSOR_ID, MODULE_HOT_SWAP, 0x00, 0x1F, 0x00, 0x1C, 0xC0, 0, 0, "HotSwap"}
	};

	Sdr_Init(pSdr, pMmc->g_pSdrHeader, MAX_SDR_COUNT);
	
	DevLocatorRecord_Init(&pMmc->m_DevicLocatorRecord
		, DEV_LOCATOR_RECORD_ID
		, g_Owerner_IPMB_Addr
		, g_Owerner_Sit_Num
		, "MMC");
	
	Sdr_Add(pSdr, (SdrHeader*)&pMmc->m_DevicLocatorRecord);
	
	CompactSensorRecord_Init(&pMmc->m_HandleCompactSensorRecord
		, HOTSWAP_RECORD_ID
		, &g_SensorItem[0]
		, g_Owerner_IPMB_Addr
		, g_Owerner_Sit_Num
		);
	Sdr_Add(pSdr, (SdrHeader*)&pMmc->m_HandleCompactSensorRecord);
	
	for(i = 0; i < MAX_COMPACT_SENSOR_RECORD; i++)
	{
		if(pConfigInfo->m_pCompactSensorRecord[i])
		{
			Sdr_Add(pSdr, (SdrHeader*)pConfigInfo->m_pCompactSensorRecord[i]);
		}
		else
		{
			break;
		}
	}
	for(i = 0; i < MAX_FULL_SENSOR_RECORD; i++)
	{
		if(pConfigInfo->m_pFullSensorRecord[i])
		{
			Sdr_Add(pSdr, (SdrHeader*)pConfigInfo->m_pFullSensorRecord[i]);
		}
		else
		{
			break;
		}
	}
	
	pSdr->m_SensorRecordCount = Sdr_GetSdrCount(pSdr, True);
	pSdr->m_RecordCount =  Sdr_GetSdrCount(pSdr, False);

	if(Sdr_Verify(pSdr))
	{
		return ERR_SUCCESS;
	}
	else
	{
		return ERR_FAILED;
	}
}

int Mmc_ReadFruDevice(FruInfo* pFruInfo, uint32 offset, uint8* pData, uint16 len)
{
	int counter = 0;
	int nReadedLen = 0;
    static uint8 txCmd[3] = {0};
    static uint8 buff[FRU_PAGE_SIZE + 1] = {0};

	Assert(len <= 32);
	
    txCmd[0] = AT24C64;
    txCmd[1] = (uint8)(offset>>8);
    txCmd[2] = (uint8)offset;
	
	do
	{
	    nReadedLen = I2c_Device_Read(I2C_CHANNEL_LOCAL
                               , txCmd
                               , sizeof(txCmd)
                               , buff
                               , len + 1);
		if(nReadedLen==0)
		{
			Trace("%s(), nReadedLen = %d", _FUNC_, nReadedLen);
			sleep(10);
		}
		if(++counter > 100)
		{
			Trace("%s, nReadedLen = %d", _FUNC_, nReadedLen);
			return -1;
		}
		
	}while(nReadedLen==0);
	
	memcpy(pData, &buff[1], len);
	return nReadedLen - 1;
}

int Mmc_WriteFruDevice(FruInfo* pFruInfo, uint32 offset, const uint8* pData, uint16 len)
{
	int nRet = 0;
	int counter = 0;
	uint8 buff[FRU_PAGE_SIZE + 3]= {0};

	Assert(len <= FRU_PAGE_SIZE + 3);

    buff[0] = AT24C64;
	buff[1] = (uint8)(offset>>8);
	buff[2] = (uint8)offset;
	memcpy(&buff[3], pData, len);
	
	do
	{
		if(I2C_WORKSTA_IDLE == I2c_WorkState_Get(I2C_CHANNEL_LOCAL))
		{
			nRet = I2C_TxData(I2C_CHANNEL_LOCAL, len+3, buff);
			if(gDelay) sleep(gDelay);
			if(LPC_FAILURE == nRet )
			{
				return 0;
			}
			else
			{
				break;
			}
		}
		
		if(++counter > 1000)
		{
			return 0;
		}
		
	}while(1);
	
	return len;
}

static int Mmc_ReadSdrDevice(FruInfo* pFruInfo, uint32 offset, uint8* pData, uint16 len)
{
	offset += SDR_REPOSITORY_ADDR;
	return Mmc_ReadFruDevice(pFruInfo, offset, pData, len);
}

static int Mmc_WriteSdrDevice(FruInfo* pFruInfo, uint32 offset, const uint8* pData, uint16 len)
{
	offset += SDR_REPOSITORY_ADDR;
	return Mmc_WriteFruDevice(pFruInfo, offset, pData, len);
}

int Mmc_InitLinkDriver(Mmc* pMmc)
{
    int ret = 0;
	
    //Init Iic local channel controler
    ret = I2C_Config( I2C_CHANNEL_LOCAL, 0, 0);
	Assert(ret == ERR_SUCCESS);

    ret = I2C_RegClient( I2C_CHANNEL_LOCAL
                                            ,EVENT_TXOK_MASK|EVENT_RXOK_MASK,
                                            (void *)MMC_revI2cRetn, 
                                            1, 
                                            &g_Owerner_IPMB_Addr);

	Assert(ret == ERR_SUCCESS);

	return ret;
}

void Mmc_InitUartIpmb(Mmc* pMmc)
{
	Ipmb* pIpmb = &pMmc->m_UartIpmb;
	
	//Init ipmb
	Ipmb_Init(pIpmb
		, I2C_CHANNEL_UARTIPMB
		, Mmc_PostMsg
		, (IpmiCmdVerifyFun)IicMsg_Verify
		, (IpmiCmdGetHeaderFun)IicMsg_GetHeader
		, Cmd_DisptchPayload
		);
			
	QUEUE_init(&pMmc->m_PayloadReqDataQueue, pMmc->m_PayloadReqDataPool, sizeof(ReqData), MAX_REQDATA_COUNT);
	IicCrb_Init(&pMmc->m_UartReqCrb
		, &pMmc->m_TimerManager
		, pIpmb
		, True, g_Owerner_IPMB_Addr, g_Owerner_IPMB_Addr, CARRIET_LUN, &pMmc->m_PayloadReqDataQueue
		);
	IicCrb_Init(&pMmc->m_UartRspCrb
		, &pMmc->m_TimerManager
		, pIpmb
		, False, g_Owerner_IPMB_Addr, g_Owerner_IPMB_Addr, CARRIET_LUN, Null
		);
	
	UartTransfer_Init(&pMmc->m_UartTransfer, pIpmb, UART_ID);
}

int Mmc_HwInit(ResetState state)
{
    drv_init(state);
	sleep(100);
    //Init link layer driver
    Mmc_InitLinkDriver(&g_Mmc);

    if(Init_Max6692Confg() != ERR_SUCCESS)
    {
		TRACE_LEVEL(TRACE_MAIN, ("Init_Max6692Confg() failed.\n"));
		Set_LedAlarm_State(TRUE);
    }

#if CONFIG_WATCHDOG
    WatchDog_Init();        /* start watch dog. */
#endif

	return True;
}

void Mmc_InitIicIpmb(Mmc* pMmc)
{
    //Init transfer layer driver

    //Init ipmb
	Ipmb_Init(&pMmc->m_Ipmb
		, I2C_CHANNEL_IPMBL
		, Mmc_PostMsg
		, (IpmiCmdVerifyFun)IicMsg_Verify
		, (IpmiCmdGetHeaderFun)IicMsg_GetHeader
		, Cmd_Disptch
		);

	
	QUEUE_init(&pMmc->m_reqDataQueue, pMmc->m_reqDataPool, sizeof(ReqData), MAX_REQDATA_COUNT);
	IicCrb_Init(&pMmc->m_ReqCrb
		, &pMmc->m_TimerManager
		, &pMmc->m_Ipmb
		, True, CM_IPMB_ADDR, g_Owerner_IPMB_Addr, 0, &pMmc->m_reqDataQueue
		);
	
	IicCrb_Init(&pMmc->m_RspCrb
		, &pMmc->m_TimerManager
		, &pMmc->m_Ipmb
		, False, CM_IPMB_ADDR, g_Owerner_IPMB_Addr, 0, Null
		);
	
    IicTransfer_Init(&pMmc->m_IicTransfer, &pMmc->m_Ipmb, g_Owerner_IPMB_Addr, I2C_CHANNEL_IPMBL, 3);
}

Bool Mmc_InitFru(Mmc* pMmc)
{
	Bool bRet = True;
	FruInfo * pFruInfo = &pMmc->m_FruInfo;
	
	if(pFruInfo->m_State != FRU_VERIFY_SUCCESS)
	{
		TRACE_LEVEL(TRACE_MAIN, ("FruInfo read data.\n"));
		bRet = FruInfo_Read(pFruInfo);
		if(bRet)
		{
			bRet = FruInfo_Parse(pFruInfo);
			if(bRet)
			{
				TRACE_LEVEL(TRACE_MAIN, ("FruInfo verify success.\n"));
			}
			else
			{
				TRACE_LEVEL(TRACE_MAIN, ("FruInfo verify failed.\n"));
			}
		}
	}

	return bRet;
		
}

Bool Mmc_InitSdr(Mmc* pMmc)
{
	Sensor* pSensor = Null;
	FruInfo * pFruInfo = &pMmc->m_ConfigInfo;

	if(pFruInfo->m_State != FRU_VERIFY_SUCCESS)
	{
		TRACE_LEVEL(TRACE_MAIN, ("ConfigInfo read data.\n"));
		if(!FruInfo_Read(pFruInfo)) return False;
		
		if(!FruInfo_Parse(pFruInfo))
		{
			TRACE_LEVEL(TRACE_MAIN, ("ConfigInfo verify failed.\n"));
			return False;
		}
		else
		{
			TRACE_LEVEL(TRACE_MAIN, ("ConfigInfo verify success.\n"));
		}
		
		ConfigInfo_Init(pFruInfo);
		if(!ConfigInfo_Verify()) return False;

		if(!pMmc->m_Sdr.m_isValid)
		{
			if(ERR_SUCCESS == Mmc_CreateSdrRepository(pMmc, &g_ConfigInfo))
			{
				Mmc_CreateSensor(pMmc, &pMmc->m_Sdr);
				pSensor = Mmc_GetSensor(HOTSWAP_SENSOR_ID);
				
				if(pSensor)
				{
					pSensor->m_RawValue = HANDLE_OPENED;
					pSensor->m_ReceiverValue = pSensor->m_RawValue;
				}
			}
		}
	}

	return True;
}

void Mmc_Start(Mmc* pMmc)
{
}

void Mmc_InitData(Mmc* pMmc)
{
	Bool bRet = Mmc_InitSdr(pMmc);
	if(pMmc->m_ConfigInfo.m_State != FRU_VERIFY_SUCCESS)
	{
		//Led_SetWorkMode(&pMmc->m_Led1, OP_SHINE, 1000, 1000, LOCAL_CONTROL);
		//TRACE_LEVEL(TRACE_MAIN, ("Start Config timer, timiout=10000 ms\n"));
		SwTimer_Start(&pMmc->m_Timer, 0, 10000);
		return;
	}

	if(bRet)
	{
		Mmc_InitFru(pMmc);
		if(pMmc->m_FruInfo.m_State != FRU_VERIFY_SUCCESS)
		{
			//Led_SetWorkMode(&pMmc->m_Led1, OP_SHINE, 500, 500, LOCAL_CONTROL);
			//TRACE_LEVEL(TRACE_MAIN, ("Start Fru timer, timiout=10000 ms\n"));
			SwTimer_Start(&pMmc->m_Timer, 0, 10000);
			return;
		}
	}

	Mmc_Start(pMmc);	
}


int Mmc_Init(Mmc* pMmc)
{
	uint8 resetState = Get_Reset_Status();
    int ret = ERR_SUCCESS;
    //unsigned char rWatchDog = 0;
    E_keying* pEKeyRecord  = NULL;

	memset(pMmc, 0, sizeof(Mmc));
	
    TimerManager_Init(&pMmc->m_TimerManager);
	
	Mmc_HwInit((ResetState)resetState);

	Debug_Init();
    TRACE_LEVEL(TRACE_MAIN,("Mmc start, reset code is [%s]\r\n", resetState == POWER_ON_RESET ? "POWER_ON_RESET" : "WATCHDOG_RESET"));

	Shell_Init();
	#if TEST
	DriverEk_Init();
	#endif
   
    pMmc->m_FirmwareMasterRev = (1 << 7) | 0x02;
    pMmc->m_FirmwareSubRev = 1;
    pMmc->m_IpmiRev = 2;
    pMmc->m_DeviceRev = (1 << 7) | 2;	
	
	pMmc->m_ReceiverAddress = CM_IPMB_ADDR<<1;
    
    //Init timermanager
	SwTimer_Init(&pMmc->m_Timer, (TimeoutFun)Mmc_TimerOut, pMmc);
	Timermanager_AddTimer(&pMmc->m_TimerManager, &pMmc->m_Timer);

    Mmc_InitIicIpmb(pMmc);
    Mmc_InitUartIpmb(pMmc);

    //Init Message Queue
    QUEUE_init(&pMmc->m_msgQueue
                , (uint8*)&pMmc->m_msgQueuePool
                , sizeof(Message)
                , MSG_QUEUE_SIZE
                );

    //rWatchDog = WDMOD;

	{
		const static LedItem g_LedItems[] = 
		{
			 {BLUE_LED, COLOR_BLUE,	COLOR_BLUE, BIT_1}
			,{LED1_LED, COLOR_RED, 	COLOR_RED, 	BIT_2}
		};
		//Blue LED init
		Led_Init(&pMmc->m_BlueLed, &pMmc->m_TimerManager, &g_LedItems[0], (SetLedFun)BlueLed_Set);
		Led_SetWorkMode(&pMmc->m_BlueLed, OP_ON, 0, 0, LOCAL_CONTROL);   
		//LED1 init
		Led_Init(&pMmc->m_Led1, &pMmc->m_TimerManager, &g_LedItems[1], (SetLedFun)AlarmLed_Set);
		Led_SetWorkMode(&pMmc->m_Led1, OP_OFF, 0, 0, LOCAL_CONTROL);  
	}

	FruInfo_Init(&pMmc->m_FruInfo
		, pMmc->m_FruBuff
		, FRU_BUFFER_SIZE
		, pMmc->m_pFruRecordArray
		, 6
		, (FruAccessFun)Mmc_ReadFruDevice
		, (FruAccessFun)Mmc_WriteFruDevice);
	
	FruInfo_Init(&pMmc->m_ConfigInfo
		, pMmc->m_SdrBuff
		, SDR_BUFFER_SIZE
		, pMmc->m_pConfigRecordArray
		, 6
		, (FruAccessFun)Mmc_ReadSdrDevice
		, (FruAccessFun)Mmc_WriteSdrDevice);
	
	Mmc_InitData(pMmc);

    pEKeyRecord  = (E_keying*)FruInfo_Find(&pMmc->m_FruInfo, 0xc0,0x19);//E_KEying record id
	E_keying_Init(pEKeyRecord);

	#if TEST
	{
		uint8 temp[4]={0};
		if(ERR_SUCCESS == Read_Max6692Temperature(temp))
		{
			TRACE_LEVEL(TRACE_MAIN, ("Sensor Temp=[0x%x][0x%x][0x%x][0x%x]\n", temp[0], temp[1], temp[2], temp[3]));
		}
		else
		{
			TRACE_LEVEL(TRACE_MAIN, ("Sensor Temp read error!\n"));
		}
	}
	g_Mmc.m_isTimerDisable = 1;
    #endif
	
    return ret;
}

static void Mmc_MsgProc(uint8 msgID, uint32 param1, uint32 param2)
{
	if(MMC_MSG_INIT == msgID)
	{
		TRACE_LINE();
		Mmc_InitData(&g_Mmc);
	}
}

static void Mmc_CheckForLed1(Mmc* pMmc)
{
	Led* pLed = &pMmc->m_Led1;

	if(!QUEUE_isEmpty(&pLed->m_Queue)) return;
	
	if(pMmc->m_ConfigInfo.m_State != FRU_VERIFY_SUCCESS)
	{
		Led_SetWorkMode(&pMmc->m_Led1, OP_SHINE, 1000, 1000, LOCAL_CONTROL);
		return;
	}
	
	if(pMmc->m_FruInfo.m_State != FRU_VERIFY_SUCCESS)
	{
		Led_SetWorkMode(&pMmc->m_Led1, OP_SHINE, 500, 500, LOCAL_CONTROL);
		return;
	}

 	if(g_Owerner_Sit_Num >= MAX_AMC_SITENUM)
	{
		Led_SetWorkMode(&pMmc->m_Led1, OP_SHINE, 100, 100, LOCAL_CONTROL);
		return;
	}

	Led_SetWorkMode(&pMmc->m_Led1, OP_OFF, 0, 0, LOCAL_CONTROL);  
}

void Mmc_Run(Mmc* pMmc)
{
    //int nCount = 0;
   // int sensorCount = 0;
    int uartCount = 0;
    //int counter3  = 0;
    int testCount = 0;
    int checkLed1Counter = 0;
	uint32	Time_5S = MS_COUNT;
#if CONFIG_WATCHDOG
    FeedDog();        /* feed watch dog. */
#endif

    while(1)
    {
        Message* pMsg = Null;

        pMsg = (Message*)QUEUE_getHead(&pMmc->m_msgQueue);
        if(pMsg) 
        {

            if(OBJECT_IPMB & pMsg->m_ObjectID)
            {
                Ipmb_msgProc((Ipmb*)pMsg->m_Param1, pMsg->m_MsgID, pMsg->m_Param2);
            }
			
            if(OBJECT_MMC & pMsg->m_ObjectID)
            {
                Mmc_MsgProc(pMsg->m_MsgID, pMsg->m_Param1, pMsg->m_Param2);
            }

            QUEUE_removeHead(&pMmc->m_msgQueue);
        }

		if(pMmc->m_ReceiverAddress != INVALID_RECEIVER_ADDR)
		{
			Mmc_SensorDetect(pMmc);
		}

        if(uartCount++ >= 10)
        {
            uartCount = 0;
            Uart_RcvData_Proc(0);  
        }

        if(testCount++ == 3000000)
        {
            testCount = 0;
            
        }
#if CONFIG_WATCHDOG
        if(gWatchDogSwitch == 1)
        {
            if(counter3++>90)
            {
                counter3=0;
                FeedDog();		// feed watch dog. 
            }
        }
#endif 
        if(checkLed1Counter ++ >= Time_5S)
        {
			checkLed1Counter = 0;
			Mmc_CheckForLed1(pMmc);
        }
    }   
}


void MMC_revI2cRetn(char chanNum,char EventInd)
{
    ;
}
